package com.damei.qmui_test_demo.httpService;

import android.content.Context;
import android.widget.Toast;

import com.damei.qmui_test_demo.model.BaseResponse;
import com.damei.qmui_test_demo.utils.LogUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

/**
 * 网络请求返回处理类
 */
public abstract class HttpStringResponseHandler<T> extends StringCallback {
    private static final String TAG = "HttpStringResponseHandler";
    private Context context;
    private Class<T> cls;
    private boolean isNeedParse;
    private boolean isNeedProgress;
    private MiniLoadingDialog miniLoadingDialog;

    /**
     * 不需要解析时使用
     *
     * @param context
     * @param isNeedProgress
     */
    public HttpStringResponseHandler(Context context, boolean isNeedProgress) {
        this.context = context;
        this.isNeedProgress = isNeedProgress;
        this.miniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this.context, "加载中...");
    }

    /**
     * 需要解析时,使用
     *
     * @param context
     * @param cls
     * @param isNeedParse
     * @param isNeedProgress
     */
    public HttpStringResponseHandler(Context context, Class<T> cls, boolean isNeedParse, boolean isNeedProgress) {
        this.context = context;
        this.cls = cls;
        this.isNeedParse = isNeedParse;
        this.isNeedProgress = isNeedProgress;
        this.miniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this.context, "加载中...");
    }

    @Override
    public void onStart(Request<String, ? extends Request> request) {
        if (isNeedProgress && context != null) {
            miniLoadingDialog.show();
        }
    }

    @Override
    public void onFinish() {
        if (isNeedProgress)
            miniLoadingDialog.dismiss();
    }

    @Override
    public void onError(Response<String> response) {
        LogUtils.e("网络访问异常，请检查网络！" + response.toString());
        Toast.makeText(context, "网络访问异常，请检查网络！", Toast.LENGTH_SHORT);
        onFaile("网络访问异常，请检查网络！");
        if (isNeedProgress)
            miniLoadingDialog.dismiss();
    }

    @Override
    public void onSuccess(/*String response, int id*/Response<String> response) {
        LogUtils.i(response.body());
        if (isNeedProgress)
            miniLoadingDialog.dismiss();

        if (isNeedParse) {
            try {
                Gson gson = new Gson();
                BaseResponse data = gson.fromJson(response.body().trim(), BaseResponse.class);
                if (data.getState() == 1) {
                    String json = gson.toJson(data.getData());
                    T result = gson.fromJson(json, cls);
                    onSuccess(result);
                }
                else {
                    Toast.makeText(context, data.msg, Toast.LENGTH_SHORT);
                    onFaile(data.msg);
                }
            } catch (Exception e) {
                LogUtils.e("数据错误" + e);
                Toast.makeText(context, "数据错误，请稍后重试", Toast.LENGTH_SHORT);
                e.printStackTrace();
            }

        } else {
            onSuccessWithNoparse(response.body());
        }
    }

    /**
     * 需要解析时的回调
     *
     * @param t
     */
    public void onSuccess(T t) {
    }


    /**
     * 不需要解析时的回调
     *
     * @param result
     */
    public void onSuccessWithNoparse(String result) {
    }

    /**
     * 失败的回调
     *
     * @param res
     */
    public void onFaile(String res) {
        LogUtils.e("http  onFaile ---> " + res);
    }
}
